export function addEventBusListener(bpmnModeler) {
  const eventBus = bpmnModeler.get("eventBus"); // 需要使用eventBus
  const modeling = bpmnModeler.get("modeling");
  const elementRegistry = bpmnModeler.get("elementRegistry");
  const eventTypes = ["element.click", "element.changed", "elements.delete"]; // 需要监听的事件集合
  eventTypes.forEach(function(eventType) {
    eventBus.on(eventType, function(e) {
      // 避免根节点触发元素事件
      if (!e || e.element.type == "bpmn:Process") return;
      if (eventType === "element.changed") {
        elementChanged(eventType, e, bpmnModeler);
      } else if (eventType === "element.click") {
        let shape = e.element ? elementRegistry.get(e.element.id) : e.shape;
        if (shape.type === "bpmn:ServiceTask") {
          modeling.updateProperties(shape, {
            name: "我是serviceTask",
            customFunction: "classclassclass"
          });
        }
        console.log(e);
      } else if (eventType === "elements.delete") {
        console.log(e);
      }
      // 一般只用他的shape信息，所以只打印shape就行了
      //   var elementRegistry = that.bpmnModeler.get("elementRegistry");
      // var shape = elementRegistry.get(e.element.id); // 传递id进去
      // console.log(shape); // {Shape}
      // console.log(e.element); // {Shape}
      // console.log(JSON.stringify(shape) === JSON.stringify(e.element)); // true
    });
  });
}
function elementChanged(eventType, e, bpmnModeler) {
  let shape = getShapeId(e.element.id, bpmnModeler);
  if (!shape) {
    console.log("shape无效");
    // if (isSequenceFlow(shape.type)) {
    //   console.log("线被删除了");
    // }
  }
  // if (!isInvalid(shape.type)) {
  // }
}
// 获取id对应的shape,当删除或添加元素时会调用这个函数
function getShapeId(id, bpmnModeler) {
  let elementRegistry = bpmnModeler.get("elementRegistry");
  console.log(elementRegistry.get(id));
  return elementRegistry.get(id);
}
function isSequenceFlow(type) {
  // 判断是否是线
  return type === "bpmn:SequenceFlow";
}
function isInvalid(param) {
  // 判断是否是无效的值
  return param === null || param === undefined || param === "";
}
